package org.dromara.authcode.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.authcode.domain.vo.TPayTransVo;
import org.dromara.authcode.domain.bo.TPayTransBo;
import org.dromara.authcode.service.ITPayTransService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 支付流水
 *
 * @author Miles Meng
 * @date 2025-08-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/authcode/payTrans")
public class TPayTransController extends BaseController {

    private final ITPayTransService tPayTransService;

    /**
     * 查询支付流水列表
     */
    @SaCheckPermission("authcode:payTrans:list")
    @GetMapping("/list")
    public TableDataInfo<TPayTransVo> list(TPayTransBo bo, PageQuery pageQuery) {
        return tPayTransService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出支付流水列表
     */
    @SaCheckPermission("authcode:payTrans:export")
    @Log(title = "支付流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TPayTransBo bo, HttpServletResponse response) {
        List<TPayTransVo> list = tPayTransService.queryList(bo);
        ExcelUtil.exportExcel(list, "支付流水", TPayTransVo.class, response);
    }

    /**
     * 获取支付流水详细信息
     *
     * @param transId 主键
     */
    @SaCheckPermission("authcode:payTrans:query")
    @GetMapping("/{transId}")
    public R<TPayTransVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long transId) {
        return R.ok(tPayTransService.queryById(transId));
    }

    /**
     * 新增支付流水
     */
    @SaCheckPermission("authcode:payTrans:add")
    @Log(title = "支付流水", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TPayTransBo bo) {
        return toAjax(tPayTransService.insertByBo(bo));
    }

    /**
     * 修改支付流水
     */
    @SaCheckPermission("authcode:payTrans:edit")
    @Log(title = "支付流水", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TPayTransBo bo) {
        return toAjax(tPayTransService.updateByBo(bo));
    }

    /**
     * 删除支付流水
     *
     * @param transIds 主键串
     */
    @SaCheckPermission("authcode:payTrans:remove")
    @Log(title = "支付流水", businessType = BusinessType.DELETE)
    @DeleteMapping("/{transIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] transIds) {
        return toAjax(tPayTransService.deleteWithValidByIds(List.of(transIds), true));
    }
}
